/*
 * 作者:熊猫
 * 日期:2017 - 3 - 14 8 - 20 - 4
 *
 */

package net.lainiao.other.demo3;

/**
 * Created by Administrator on 2017/3/14.
 */
public class MyTestChild {
    public static void main(String[] args) {
        final  BizTwo bizTwo=new BizTwo();
        new Thread(new Runnable() {
            @Override
            public void run() {
                bizTwo.childDo();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                bizTwo.mainDo();
            }
        }).start();

    }
}


class BizTwo {
    int cyc = 1;

    public void childDo() {
       synchronized (this){
           while(cyc<101){
               if(cyc%2==1){

                   for (int i = 1; i <=10 ; i++) {
                       System.out.println("cyc "+cyc+" child thread do "+i);
                   }
                   cyc++;
               }
               this.notifyAll();
               try {
                   this.wait();
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
       }
    }

    public void mainDo() {
        synchronized (this){
            while(cyc<101){
                if(cyc%2==0){

                    for (int i = 1; i <= 100; i++) {
                        System.out.println("cyc "+cyc+" main thread do "+i);
                    }
                    cyc++;
                }
                this.notifyAll();
                try {
                    if(cyc<101){
                        this.wait();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}